<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <div class="RadvdStatus">
        <span style="color: #606266">{{$t('RADVD开关:')}}</span>
        <el-switch :width="45" v-model="RadvdStatus" @change="statusChange" active-color="#009966" inactive-color="#CCCCCC" class="RadvdStatus switch" active-value="start" inactive-value="stop" :active-text="$t('开')" :inactive-text="$t('关')"></el-switch>
      </div>
    </div>
    <el-table size='mini' ref="multipleTable" :data="radvdList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="PortName" :label="$t('接口名称')" align='center' min-width="200">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.PortName" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="Prefix" :label="$t('子网前缀')" align='center' min-width="200"></el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="250">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogConfigVisible(scope.row, 'set')" :class="'setting' +(scope.$index+1)">{{$t('配置')}}</el-button>
          <el-button type="primary" plain size="mini" @click="dialogConfigVisible(scope.row, 'check')" :class="'finds' +(scope.$index+1)">{{$t('查看')}}</el-button>
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <radvd-dialog v-if="radvdDialogVisible" ref="refRadvdDialogVisible"></radvd-dialog>
    <radvd-config-dialog v-if="radvdConfigDialogVisible" ref="refRadvdConfigDialogVisible"></radvd-config-dialog>
  </div>
</template>
<script>
import { getRadvdArray, delRadvdInfo, switchRadvd, getRadvdStatus } from '@/api/network/route/radvd'
import RadvdDialog from './components/RadvdDialog'
import RadvdConfigDialog from './components/RadvdConfigDialog.vue'
export default {
  name: 'radvd',
  components: {
    RadvdDialog,
    RadvdConfigDialog
  },
  data () {
    return {
      radvdDialogVisible: false,
      radvdConfigDialogVisible: false,
      radvdList: [],
      RadvdStatus: 'stop',
      // total: 0,
      name: ''
    }
  },
  created () {
    this.listData()
    getRadvdStatus().then(res => {
      if (res.status === 0) {
        this.RadvdStatus = res.info[0].RadvdStatus === 0 ? 'stop' : 'start'
      }
    })
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      var name = ''
      for (var item of data) {
        if (name === '') {
          name = item.PortName
        } else {
          name = name + '@#@' + item.PortName
        }
      }
      this.name = name
    },
    // 列表
    listData () {
      getRadvdArray().then(res => {
        this.radvdList = []
        if (res.status === 0) {
          this.radvdList = res.info
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.radvdDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refRadvdDialogVisible.init(data)
      })
    },
    // 配置按钮表单
    dialogConfigVisible (data, type) {
      this.radvdConfigDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refRadvdConfigDialogVisible.init(data, type)
      })
    },
    deleteData () {
      if (this.name) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delRadvdInfo({ PortName: this.name }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.name = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    statusChange () {
      switchRadvd({ RadvdStatus: this.RadvdStatus }).then(res => {
        if (res.status === 0) {
          this.$message({
            message: this.$t(res.msg),
            type: 'success'
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.RadvdStatus {
  float: right;
  margin-right: 20px;
  .el-switch {
    margin-left: 10px;
  }
}
</style>
